package com.google.code.drift.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * <strong>DefaultSharedPreferences</strong>的代理工具类，利用了枚举enum
 * 
 * @author zkwlx
 * 
 */
public class PreferencesProxy {

	public enum PrefKey {

		MY_NICK("userNick", "无名氏"), MY_SEX("userSex", 2), MY_ID("userId", -1), AUTO_REFRESH(
				"autoRefresh", true), PLAY_CHAT_RING("playChatRing", false), CHAT_VIBRATOR(
				"chatVibrator", true);

		private String name = "";
		private Object defaultValue = "";

		PrefKey(String name, Object defaultValue) {
			this.name = name;
			this.defaultValue = defaultValue;
		}

		public String getName() {
			return name;
		}

		public Object getDefault() {
			return defaultValue;
		}

	}

	private static SharedPreferences globalPre = null;
	private static SharedPreferences.Editor editor = null;

	public static void init(Context context, boolean clean) {

		globalPre = PreferenceManager.getDefaultSharedPreferences(context);
		editor = globalPre.edit();

		if (clean)
			editor.clear().commit();

	}

	public static String getString(PrefKey key) {
		return globalPre.getString(key.getName(), (String) key.getDefault());
	}

	public static int getInt(PrefKey key) {
		return globalPre.getInt(key.getName(), (Integer) key.getDefault());
	}

	public static boolean getBoolean(PrefKey key) {
		return globalPre.getBoolean(key.getName(), (Boolean) key.getDefault());
	}

	public static void putString(PrefKey key, String value) {
		editor.putString(key.getName(), value);
		editor.commit();
	}

	public static void putInt(PrefKey key, int value) {
		editor.putInt(key.getName(), value);
		editor.commit();
	}

	public static void putBoolean(PrefKey key, boolean value) {
		editor.putBoolean(key.getName(), value);
		editor.commit();
	}

}
